<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>药品信息管理</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style type="text/css">
        .layui-form-select dl {
            max-height: 180px;
        }
    </style>
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">药品名称:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="请输入药品名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">药品编码:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="code" placeholder="请输入药品编码" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态:</label>
                    <div class="layui-input-inline">
                        <select id="state" name="state">
                            <option value="">可用状态</option>
                            <option value="1">正常</option>
                            <option value="2">停用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">药品类型:</label>
                        <div class="layui-input-inline">
                            <select id="drugType" name="drugType">
                                <option value="">请选择药品类型</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">生产厂家:</label>
                        <div class="layui-input-inline">
                            <select id="production" name="production">
                                <option value="">请选择生产厂家</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">处方类型:</label>
                        <div class="layui-input-inline">
                            <select id="preType" name="preType">
                                <option value="">请选择处方类型</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="drugInfo-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<!--添加和修改弹出层-->
<div style="display: none;padding: 3px" id="saveOrUpdateDiv">
    <form class="layui-form" id="dataFrm" lay-filter="dataFrm" action="">
        <div class="layui-form-item" style="margin-top: 10px;">
            <div class="layui-inline">
                <label class="layui-form-label">药品名称:</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="id">
                    <input type="text" name="name" lay-verify="required" autocomplete="off"
                           placeholder="请输入药品名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">药品编码:</label>
                <div class="layui-input-inline">
                    <input type="text" name="code" lay-verify="required" autocomplete="off"
                           placeholder="请输入药品编码" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">药品类型:</label>
                <div class="layui-input-inline">
                    <select id="drugType2" name="drugType">
                        <option value="">药品类型</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">处方类型:</label>
                <div class="layui-input-inline">
                    <select id="preType2" name="preType">
                        <option value="">处方类型</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">生产厂家:</label>
                <div class="layui-input-inline">
                    <select id="production2" name="production">
                        <option value="">生产厂家</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">单位:</label>
                <div class="layui-input-inline">
                    <input type="text" name="company" lay-verify="required" autocomplete="off"
                           placeholder="请输入单位" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">换算量:</label>
                <div class="layui-input-inline">
                    <input type="text" name="conversion" lay-verify="required" autocomplete="off"
                           placeholder="请输入换算量" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">处方价格:</label>
                <div class="layui-input-inline">
                    <input type="text" name="price" lay-verify="required|number" autocomplete="off"
                           placeholder="请输入处方价格" class="layui-input">
                    <input type="hidden" name="inventory" value="200" lay-verify="required|number" autocomplete="off"
                           placeholder="请输入库存量" class="layui-input">
                    <input type="hidden" name="early" value="0" lay-verify="required|number" autocomplete="off"
                           placeholder="请输入预警值" class="layui-input">
                </div>
            </div>
        </div>
<!--        <div class="layui-form-item">-->
<!--            <div class="layui-inline">-->
<!--                <label class="layui-form-label">库存量:</label>-->
<!--                <div class="layui-input-inline">-->
<!--                   -->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="layui-inline">-->
<!--                <label class="layui-form-label">预警值:</label>-->
<!--                <div class="layui-input-inline">-->
<!--                    -->
<!--                </div>-->
<!--            </div>-->
<!--        </div>-->

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">状态:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="state" value="1" title="正常" checked>
                    <input type="radio" name="state" value="2" title="停用">
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="drugInfo-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</div>

<!--数据表格-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="drugInfo-table" lay-filter="drugInfo-table"></table>
    </div>
</div>

<!--表头-->
<script type="text/html" id="drugInfo-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        批量删除
    </button>
</script>

<!--行工具栏-->
<script type="text/html" id="drugInfo-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        //加载药品类型的下拉框
        $.get("/drugInfo/loadDrugType", function (data) {
            let drugType = $("#drugType");
            let html = '<option value="">药品类型</option>'
            $.each(data.data, function (i, item) {
                html += '<option value="' + item.id + '">' + item.name + '</option>'
            });
            drugType.html(html);
            form.render();
        });


        //加载生产厂家的下拉框
        $.get("/drugInfo/loadProduction", function (data) {
            let production = $("#production");
            let html = '<option value="">药品类型</option>'
            $.each(data.data, function (i, item) {
                html += '<option value="' + item.id + '">' + item.name + '</option>'
            });
            production.html(html);
            form.render();
        });

        //加载处方类型的下拉框
        $.get("/drugInfo/loadPreType", function (data) {
            let preType = $("#preType");
            let html = '<option value="">药品类型</option>'
            $.each(data.data, function (i, item) {
                html += '<option value="' + item.id + '">' + item.name + '</option>'
            });
            preType.html(html);
            form.render();
        });

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '药品ID',
                    field: 'id',
                    hide: true,
                    width: '70',
                    align: 'center',
                },
                {
                    title: '药品名称',
                    field: 'name',
                    width: '100',
                    align: 'center'
                },
                {
                    title: '药品编码',
                    field: 'code',
                    width: '100',
                    align: 'center',
                },
                {
                    title: '生产厂家',
                    field: 'productionName',
                    width: '190',
                    align: 'center',
                },
                {
                    title: '药品类型',
                    width: '100',
                    field: 'drugTypeName',
                    align: 'center',
                },
                {
                    title: '处方类型',
                    width: '100',
                    field: 'preTypeName',
                    align: 'center',
                },
                {
                    title: '单位',
                    width: '70',
                    field: 'company',
                    align: 'center',
                },
                {
                    title: '处方价格',
                    width: '100',
                    field: 'price',
                    align: 'center',
                },
                // {
                //     title: '库存量',
                //     width: '80',
                //     field: 'inventory',
                //     align: 'center',
                // },
                // {
                //     title: '预警值',
                //     width: '80',
                //     field: 'early',
                //     align: 'center',
                // },
                {
                    title: '换算量',
                    width: '70',
                    field: 'conversion',
                    align: 'center',
                },
                {
                    title: '状态',
                    field: 'state',
                    width: '100',
                    align: 'center',
                    templet: function (d) {
                        let strCheck = d.state == "1" ? "checked" : "";
                        return '<input type="checkbox" name="state" lay-skin="switch" lay-text="正常|停用" lay-filter="drugInfo-enable" ' + strCheck + ' id=' + d.id + '>';
                    }
                },
                {
                    title: '操作',
                    toolbar: '#drugInfo-bar',
                    align: 'center',
                    width: '110'
                }
            ]
        ]

        var tableIns = table.render({
            elem: '#drugInfo-table',
            url: '/drugInfo/loadAllDrugInfoPage',
            page: true,
            cols: cols,
            toolbar: '#drugInfo-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print'],
            response: {
                statusCode: 200
            },
            done: function (res, curr, count) {
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            },
        });

        //行监听
        table.on('tool(drugInfo-table)', function (obj) {
            if (obj.event === 'remove') {
                remove(obj);
            } else if (obj.event === 'edit') {
                edit(obj);
            }
        });

        //表头监听
        table.on('toolbar(drugInfo-table)', function (obj) {
            if (obj.event === 'add') {
                add();
            } else if (obj.event === 'batchRemove') {
                batchRemove(obj);
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        //表格监听
        form.on('submit(drugInfo-query)', function (data) {
            table.reload('drugInfo-table', {
                where: data.field
            })
            return false;
        });

        //   行 点击监听
        form.on('switch(drugInfo-enable)', function (obj) {
            var id = $(this).attr('id');
            var state = '';
            obj.elem.checked ? state = '1' : state = '2';
            $.ajax({
                url: "/drugInfo/updateDrugInfoState/" + id + "/" + state,
                dataType: 'json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        });
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            })

        });

        //添加
        var mainIndex;
        var url;

        function add() {
            mainIndex = layer.open({
                type: 1,
                title: '添加药品信息',
                shade: 0.1,
                area: ['690px', '490px'],
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/drugInfo/addDrugInfo";
                    $("#dataFrm")[0].reset();

                    //加载药品类型的下拉框
                    $.ajax({
                        url: "/drugInfo/loadDrugType",
                        type: "GET",
                        async: false,
                        success: function (data){
                            let drugType = $("#drugType2");
                            let html = '<option value="">药品类型</option>'
                            $.each(data.data, function (i, item) {
                                html += '<option value="' + item.id + '">' + item.name + '</option>'
                            });
                            drugType.html(html);
                        }
                    })


                    //加载生产厂家的下拉框
                    $.ajax({
                        url:"/drugInfo/loadProduction",
                        type: "GET",
                        async: false,
                        success: function (data){
                            let production = $("#production2");
                            let html = '<option value="">生产厂家</option>'
                            $.each(data.data, function (i, item) {
                                html += '<option value="' + item.id + '">' + item.name + '</option>'
                            });
                            production.html(html);
                        }
                    })

                    //加载处方类型的下拉框
                    $.ajax({
                        url: "/drugInfo/loadPreType",
                        type: "GET",
                        async: false,
                        success: function (data){
                            let preType = $("#preType2");
                            let html = '<option value="">处方类型</option>'
                            $.each(data.data, function (i, item) {
                                html += '<option value="' + item.id + '">' + item.name + '</option>'
                            });
                            preType.html(html);
                        }

                    })
                    form.render();
                }
            });
        }


        //修改
        function edit(obj) {
            mainIndex = layer.open({
                type: 1,
                title: '修改药品信息',
                shade: 0.1,
                area: ['690px', '490px'],
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/drugInfo/updateDrugInfo"
                    $("#dataFrm")[0].reset();
                    form.val("dataFrm", obj.data);

                    //加载药品类型的下拉框
                    $.ajax({
                        url: "/drugInfo/loadDrugType",
                        type: "GET",
                        async: false,
                        success :function (data){
                            let drugType = $("#drugType2");
                            let html = '<option value="">药品类型</option>'
                            $.each(data.data, function (i, item) {
                                if (item.id === obj.data.drugType) {
                                    html += '<option value="' + item.id + '" selected=selected>' + item.name + '</option>'
                                } else {
                                    html += '<option value="' + item.id + '">' + item.name + '</option>'
                                }
                            });
                            drugType.html(html);
                        }
                    })

                    //加载生产厂家的下拉框
                    $.ajax({
                        url: "/drugInfo/loadProduction",
                        type: "GET",
                        async: false,
                        success :function (data){
                            let production = $("#production2");
                            let html = '<option value="">生产厂家</option>'
                            $.each(data.data, function (i, item) {
                                if (item.id === obj.data.production) {
                                    html += '<option value="' + item.id + '" selected=selected>' + item.name + '</option>'
                                } else {
                                    html += '<option value="' + item.id + '">' + item.name + '</option>'
                                }
                            });
                            production.html(html);
                        }
                    })

                    //加载处方类型的下拉框
                    $.ajax({
                        url: "/drugInfo/loadPreType",
                        type: "GET",
                        async: false,
                        success :function (data){
                            let preType = $("#preType2");
                            let html = '<option value="">处方类型</option>'
                            $.each(data.data, function (i, item) {
                                if (item.id == obj.data.preType) {
                                    html += '<option value="' + item.id + '" selected=selected>' + item.name + '</option>'
                                } else {
                                    html += '<option value="' + item.id + '">' + item.name + '</option>'
                                }
                            });
                            preType.html(html);
                        }
                    })
                    form.render();
                }
            });
        }


        //监听提交按钮
        form.on('submit(drugInfo-save)', function (data) {
            $.ajax({
                url: url,
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            layer.close(mainIndex);//关闭当前页
                            tableIns.reload();
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        //删除
        function remove(obj) {
            layer.confirm('确定要删除?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/drugInfo/deleteDrugInfo/" + obj.data['id'],
                    dataType: 'json',
                    type: 'DELETE',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        //批量删除
        function batchRemove(obj) {

            var ids = common.checkField(obj, 'id');

            if (ids === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/drugInfo/bathDeleteDrugInfo/" + ids,
                    dataType: 'json',
                    type: 'DELETE',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('drugInfo-table');
        }

    })
</script>
</body>
</html>
